package a66weding.com.jiehuntong.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.math.BigDecimal;
import java.util.HashMap;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.MyActivityManager;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.common.utils.Utils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.NetStatusUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class NewPassWordLoginActivity extends AppCompatActivity implements View.OnClickListener, OnNetResultListener {
    private ImageView img_new_login_phone;
    private TextInputEditText tiet_new_login_phone;
    private TextInputLayout til_new_login_phone;
    private TextInputEditText tiet_new_login_pwd;
    private TextInputLayout til_new_login_pwd;
    private Button btn_new_login;
    private ImageButton imgbtn_new_login_qq;
    private ImageButton imgbtn_new_login_wechat;
    private ImageButton imgbtn_new_login_sina;
    private RelativeLayout activity_new_login;
    private LinearLayout ll_new_login_back;
    private User userBean;
    public static int LOGIN_OK = 200;
    private static final int WHAT_EXIT_FLAG = 0x128;//再次点击返回按钮退出应用
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        //去掉标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_new_pass_word_login);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("NewPassWordLoginActivity", this);
        initView();
    }

    private void initView() {
        img_new_login_phone = (ImageView) findViewById(R.id.img_new_login_phone);
        tiet_new_login_phone = (TextInputEditText) findViewById(R.id.tiet_new_login_phone);
        til_new_login_phone = (TextInputLayout) findViewById(R.id.til_new_login_phone);
        tiet_new_login_pwd = (TextInputEditText) findViewById(R.id.tiet_new_login_pwd);
        til_new_login_pwd = (TextInputLayout) findViewById(R.id.til_new_login_pwd);
        btn_new_login = (Button) findViewById(R.id.btn_new_login);
        imgbtn_new_login_qq = (ImageButton) findViewById(R.id.imgbtn_new_login_qq);
        imgbtn_new_login_wechat = (ImageButton) findViewById(R.id.imgbtn_new_login_wechat);
        imgbtn_new_login_sina = (ImageButton) findViewById(R.id.imgbtn_new_login_sina);
        activity_new_login = (RelativeLayout) findViewById(R.id.activity_new_login);
        btn_new_login.setOnClickListener(this);
        imgbtn_new_login_qq.setOnClickListener(this);
        imgbtn_new_login_wechat.setOnClickListener(this);
        imgbtn_new_login_sina.setOnClickListener(this);
        ll_new_login_back = (LinearLayout) findViewById(R.id.ll_new_login_back);
        ll_new_login_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_new_login_back://返回
                finish();
                break;
            case R.id.btn_new_login://登陆按钮
                submit();
                break;
            case R.id.imgbtn_new_login_qq://QQ
                qqLogin();
                break;
            case R.id.imgbtn_new_login_wechat://微信
                weChatLogin();
                break;
            case R.id.imgbtn_new_login_sina://新浪
                sinaLogin();
                break;
        }
    }

    /**
     * 新浪微博登录
     */
    public void sinaLogin() {
        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //输出所有授权信息
                String s = arg0.getDb().exportData();
                Log.i("新浪微博", "onComplete: " + s);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
            }
        });
        //authorize与showUser单独调用一个即可
        // weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
        weibo.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }

    /**
     * QQ登录
     */
    public void qqLogin() {
        Platform weibo = ShareSDK.getPlatform(QQ.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //输出所有授权信息
                String s = arg0.getDb().exportData();
                Log.i("QQ", "onComplete: " + s);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
            }
        });
        //authorize与showUser单独调用一个即可
        // weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
        weibo.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }

    /**
     * 微信登录
     */
    public void weChatLogin() {
        Platform weibo = ShareSDK.getPlatform(Wechat.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //输出所有授权信息
                String s = arg0.getDb().exportData();
                Log.i("微信", "onComplete: " + s);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
            }
        });
        //authorize与showUser单独调用一个即可
        // weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
        weibo.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }

    /**
     * 提交信息登录
     */
    private void submit() {
        String phone = tiet_new_login_phone.getText().toString().trim();
        String pwd = tiet_new_login_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            CUtils.showToast("手机号码不能为空！");
            return;
        }
        if (!Utils.isPhone(phone)) {
            CUtils.showToast("手机号码格式有误！");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            CUtils.showToast("密码不能为空！");
            return;
        }
        if (pwd.length() < 6) {
            CUtils.showToast("密码必须为6位以上！");
            return;
        }
        if (pwd.contains(" ")) {
            CUtils.showToast("密码不能包含空格！");
            return;
        }
        boolean networkAvailable = NetStatusUtil.isNetworkAvailable(this);
        if (networkAvailable) {
            String url = Url.PASSWORDLOGIN + "&nvc_MobilePhone=" + phone + "&nvc_PassWord=" + pwd;
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, NewPassWordLoginActivity.this, User.class, "PasswordLogin");
            HttpHandleUtil.executeCommonPost(NewPassWordLoginActivity.this, httpRequestBean);
        } else {
            CUtils.showToast("当前无网络!请检查");
        }
    }

    /**
     * 保存用户信息
     *
     * @param user
     */
    private void saveUserInfo(User user) {
        if (user != null) {
            DataWareHourse.getInstance().setmUser(user);
            User.DataBean dataBean = user.getData().get(0);
            ((GlobalApplication) getApplication()).setLogin(true);
            CacheUtils.setBooleanToCache(this, CacheUtils.islogin, true);
            CacheUtils.saveObjectToTextFile(RequestCode.TOLOGIN_ACTIVITY_REQUESTCODE, user);
            CacheUtils.setStringToCache(this, CacheUtils.phoneNum, dataBean.getNvc_MobilePhone());
            CacheUtils.setStringToCache(this, CacheUtils.verification, tiet_new_login_pwd.getText().toString().trim());
        }
    }


    private void setLoginFinishResult() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof User) {
            userBean = (User) data;
            switch (userBean.getResCode()) {
                case "0000":
                    ((GlobalApplication) getApplication()).setLogin(true);
                    saveUserInfo(userBean);
                    CUtils.showToast("登陆成功");
                    setLoginFinishResult();
                    break;
                default:
                    CUtils.showToast(userBean.getMsg());
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }
}
